package com.jiangyao.common.configs;

import org.dozer.DozerConverter;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class LocalDateToDateDozerConverter extends DozerConverter<LocalDate, Date> {

	public LocalDateToDateDozerConverter() {
		super(LocalDate.class, Date.class);
	}

	@Override
	public LocalDate convertFrom(Date source, LocalDate destination) {
		Instant instant = source.toInstant();
		return instant.atZone(ZoneId.systemDefault()).toLocalDate();
	}

	@Override
	public Date convertTo(LocalDate source, Date destination) {
		LocalDate localDate = LocalDate.of(source.getYear(), source.getMonth(), source.getDayOfMonth());
		return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
	}

}